﻿using System;

namespace RPC {
	public abstract class RPCMessage : IRPC {
		#region Fields
		private readonly RPCID _id;
		private RPCBody _body;
		private RPCFormat _format;
		#endregion
		#region Constructor
		/// <summary>
		/// Initializes a new instance of the <see cref="RPCMessage"/> class.
		/// </summary>
		public RPCMessage() : this(RPCBody.Checksum,RPCFormat.None){
			
		}
		/// <summary>
		/// Initializes a new instance of the <see cref="RPCMessage"/> class.
		/// </summary>
		/// <param name="body">The body.</param>
		/// <param name="format">The format.</param>
		public RPCMessage(RPCBody body, RPCFormat format){
			_body = body;
			_format = format;

			_id = RPCID.ParseAttribute(this);
		}
		#endregion
		#region Public
		/// <summary>
		/// Returns a <see cref="System.String"/> that represents this instance.
		/// </summary>
		/// <returns>
		/// A <see cref="System.String"/> that represents this instance.
		/// </returns>
		public override string ToString() {
			return String.Format("[{0}] {1}",this.ID.ToString(),this.GetType().Name);
		}
		/// <summary>
		/// Writes the data.
		/// </summary>
		/// <param name="w">The writer.</param>
		public abstract void WriteData(RPCWriter w);
		/// <summary>
		/// Reads the data.
		/// </summary>
		/// <param name="r">The reader.</param>
		public abstract void ReadData(RPCReader r);
		#endregion
		#region Property 
		/// <summary>
		/// Gets the ID.
		/// </summary>
		/// <value>The ID.</value>
		public RPCID ID {
			get {
				return _id;
			}
		}
		/// <summary>
		/// Gets or sets the body of the message.
		/// </summary>
		/// <value>The body.</value>
		public RPCBody Body {
			get {
				return _body;
			}
			set {
				_body = value;
			}
		}
		/// <summary>
		/// Gets or sets the format of the message.
		/// </summary>
		/// <value>The format.</value>
		public RPCFormat Format {
			get {
				return _format;
			}
			set {
				_format = value;
			}
		}
		#endregion
	}
}
